package cn.bluech.template.reflect;

import android.util.Log;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Description:
 * Create by Codeant on 2019/1/29.
 */
public class MyProxy implements InvocationHandler {
    private static final String TAG = MyProxy.class.getSimpleName();
    private static final String ClassName = "cn.bluech.template.reflect.hide.IPrivate";

    public static Object newInstance() throws ClassNotFoundException {
        Class[] interfaces = new Class[]{Class.forName(ClassName)};
        return Proxy.newProxyInstance(MyProxy.class.getClassLoader(),interfaces, new MyProxy());
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String methodName = method.getName();
        Class<?> returnType = method.getReturnType();
        // 使用json解析数据后返回.
//        Object parseObject = JSON.parseObject("{res:\"ff\"}",returnType);

        Log.d(TAG,"call fun: "+methodName);
        Log.d(TAG,"call fun returnType: "+returnType.getName());
        switch(methodName){
            case "interface1":
                break;
            case "interface2":

                return 8;
            default:
                break;
        }

        return null;
    }
}